using UnityEngine;
using System.Collections;

public class SuperMeatBoyMechanic : MonoBehaviour {

	public Axis _AxisLeft;
	public Axis _AxisRight;
	public Axis _AxisJump;
	public float moveSpeed;
	public float jumpAmount;
	public float velocityLimit;
	private float forceInWall;
	
	void Update () 
	{
		if(Input.GetAxis("Horizontal") > 0.5 && !_AxisRight.isGrounded)
		{
			rigidbody.AddForce(moveSpeed,0,0);
			
			if(rigidbody.velocity.x > velocityLimit)
			{
				rigidbody.velocity = new Vector3(velocityLimit,rigidbody.velocity.y,rigidbody.velocity.z);
			}
		}
		
		if(Input.GetAxis("Horizontal") < -0.5 && !_AxisLeft.isGrounded)
		{
			rigidbody.AddForce(-moveSpeed,0,0);
			
			if(rigidbody.velocity.x < -velocityLimit)
			{
				rigidbody.velocity = new Vector3(-velocityLimit,rigidbody.velocity.y,rigidbody.velocity.z);
			}
		}
		

		if(_AxisLeft.isGrounded || _AxisRight.isGrounded)
		{
			Physics.gravity = new Vector3(0,-1f,0);
		}

		if(!_AxisLeft.isGrounded && !_AxisRight.isGrounded)
		{
			Physics.gravity = new Vector3(0,-9.81f,0);
		}

		
		if(Input.GetKeyDown(KeyCode.Space) && (_AxisJump.isGrounded ||_AxisLeft.isGrounded ||_AxisRight.isGrounded))
		{
			Jump();
		}
	}
	
	void Jump()
	{
		if(_AxisLeft.isGrounded)
		{
			if(Input.GetAxis("Horizontal") > 0)
			{
				forceInWall = 10;
			}	
			
			if(Input.GetAxis("Horizontal") < 0)
			{
				forceInWall = 5;
			}	
		}
		
		if(_AxisRight.isGrounded)
		{
			if(Input.GetAxis("Horizontal") < 0)
			{
				forceInWall = -10;
			}	
			
			if(Input.GetAxis("Horizontal") > 0)
			{
				forceInWall = -5;
			}	
		}
		rigidbody.AddForce(forceInWall,jumpAmount,0,ForceMode.Impulse);
		
		forceInWall = 0;
	}
	

}
